import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import com.ib.client.Contract;

class PortfolioTable extends AbstractTableModel {
    Vector m_allData = new Vector();

    void updatePortfolio(Contract contract, int position, double marketPrice, double marketValue,
                         double averageCost, double unrealizedPNL, double realizedPNL, String accountName) {
    	 String msg = "Position is " + position + " Price is " + marketPrice;

         PortfolioTableRow newData =
         new PortfolioTableRow(contract, position, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL, accountName);
         int size = m_allData.size();
         for ( int i = 0; i < size; i++ ) {
             PortfolioTableRow test = (PortfolioTableRow)m_allData.get(i);
             if ( test.m_contract.equals(newData.m_contract) ) {
                 if ( newData.m_position == 0 )
                     m_allData.remove(i);
                 else
                     m_allData.set(i, newData);

                 fireTableDataChanged();
                 return;
             }
         }

         m_allData.add(newData);
         fireTableDataChanged();
    }

    void reset() {
        m_allData.clear();
    }

    public int getRowCount() {
        return m_allData.size();
    }

    public int getColumnCount() {
        return 13;
    }

    public Object getValueAt(int r, int c) {
        return ((PortfolioTableRow) m_allData.get(r)).getValue(c);
    }

    public boolean isCellEditable(int r, int c) {
        return false;
    }

    public String getColumnName(int c) {
        switch(c) {
            case 0:
                return "Symbol";
            case 1:
                return "SecType";
            case 2:
                return "Expiry";
            case 3:
                return "Strike";
            case 4:
                return "Right";
            case 5:
            	return "Multiplier";
            case 6:
            	return "Exchange";
            case 7:
                return "Currency";
            case 8:
                return "Position";
            case 9:
                return "Market Price";
            case 10:
                return "Market Value";
            case 11:
                return "Average Cost";
            case 12:
                return "Unrealized P&L";
            case 13:
                return "Realized P&L";
            case 14:
                return "Account Name";
            default:
                return null;
        }
    }

    class PortfolioTableRow {
        Contract m_contract;
        int      m_position;
        double   m_marketPrice;
        double   m_marketValue;
        double   m_averageCost;
        double   m_unrealizedPNL;
        double   m_realizedPNL;
        String   m_accountName;

        PortfolioTableRow( Contract contract, int position, double marketPrice,
            double marketValue, double averageCost, double unrealizedPNL,
            double realizedPNL, String accountName) {
            m_contract = contract;
            m_position = position;
            m_marketPrice = marketPrice;
            m_marketValue = marketValue;
            m_averageCost = averageCost;
            m_unrealizedPNL = unrealizedPNL;
            m_realizedPNL = realizedPNL;
            m_accountName = accountName;
        }

        Object getValue(int c) {
            switch(c) {
                case 0:
                    return m_contract.m_symbol;
                case 1:
                    return m_contract.m_secType;
                case 2:
                    return m_contract.m_expiry;
                case 3:
                    return m_contract.m_expiry == null ? null : "" + m_contract.m_strike;
                case 4:
                    return (m_contract.m_right != null && m_contract.m_right.equals("???")) ? null : m_contract.m_right;
                case 5:
                	return m_contract.m_multiplier;
                case 6:
                	return (m_contract.m_primaryExch != null ? m_contract.m_primaryExch : "");
                case 7:
                    return m_contract.m_currency;
                case 8:
                    return "" + m_position;
                case 9:
                    return "" + m_marketPrice;
                case 10:
                    return "" + m_marketValue;
                case 11:
                    return "" + m_averageCost;
                case 12:
                    return "" + m_unrealizedPNL;
                case 13:
                    return "" + m_realizedPNL;
                case 14:
                    return m_accountName;
                default:
                    return null;
            }
        }
    }
}